import pandas as pd
import warnings

warnings.filterwarnings("ignore")

def get_chart_data_optimized(df):
    """
    基于传入的DataFrame生成图表数据（优化版）
    参数: df - 预加载的销售数据DataFrame
    """
    # 核心业务逻辑
    df_manufacturer = df.groupby(['日期', '厂商'])['销量'].sum().reset_index()
    df_manufacturer['排名'] = df_manufacturer.groupby('日期')['销量'].rank(ascending=False)
    df_top10 = df_manufacturer[df_manufacturer['排名'] <= 10].sort_values(['日期', '排名'])

    # 构建图表数据结构
    dates = df_top10['日期'].unique().tolist()
    chart_data = {'dates': dates, 'data': {}}
    for date in dates:
        month_data = df_top10[df_top10['日期'] == date].sort_values('销量', ascending=False)
        chart_data['data'][date] = {
            'groups': month_data['厂商'].tolist(),
            'sales': month_data['销量'].tolist()
        }
    return chart_data